/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI;

/**
 *
 * @author GUR04686
 */
public class EventListener implements java.awt.event.ActionListener {
    
    @Override public void actionPerformed(java.awt.event.ActionEvent evt) {
        ExtendedMenuItem mItem = (ExtendedMenuItem)evt.getSource();
        
        MainWindow window_obj = (MainWindow)mItem.get_app_window_object();
        javax.swing.tree.DefaultMutableTreeNode cur_node = (javax.swing.tree.DefaultMutableTreeNode)window_obj.get_current_tree_node();
        javax.swing.tree.DefaultMutableTreeNode childNode;
        javax.swing.tree.DefaultMutableTreeNode parentNode;
        int node_index;
                
        switch(mItem.get_performer_id()) {
            case TREE_ROOT_MENU_ITEM_NEW_SCHEMA_MODULE:
                childNode = new javax.swing.tree.DefaultMutableTreeNode("New Schema Module");
                childNode.setUserObject(new UI.NodeData(new DataLogic.ModuleInfo("New Schema Module" , DataLogic.TypeEnums.SCHEMA_MODULE), childNode));
                window_obj.insert_node(childNode, cur_node, 0);
                window_obj.edit_node(childNode);
                break;
            case TREE_ROOT_MENU_ITEM_NEW_ENUM_MODULE:
                childNode = new javax.swing.tree.DefaultMutableTreeNode("New Enum Module");
                childNode.setUserObject(new UI.NodeData(new DataLogic.ModuleInfo("New Enum Module", DataLogic.TypeEnums.ENUM_MODULE), childNode));                
                window_obj.insert_node(childNode, cur_node, 0);
                window_obj.edit_node(childNode);
                break;
            case TREE_SCHEMA_MODULE_MENU_ITEM_NEW_DATA_TYPE:
                childNode = new javax.swing.tree.DefaultMutableTreeNode("New Schema");
                childNode.setUserObject(new UI.NodeData(new DataLogic.SchemaInfo("New Schema", 0, 0, DataLogic.TypeEnums.DATA_TYPE), childNode));
                window_obj.insert_node(childNode, cur_node, 0);
                window_obj.edit_node(childNode);
                break;
            case TREE_ENUM_MODULE_MENU_ITEM_NEW_ENUM_DEFINITION:
                childNode = new javax.swing.tree.DefaultMutableTreeNode("New Enumeration");
                childNode.setUserObject(new UI.NodeData(new DataLogic.EnumsInfo("New Enumeration", 0, DataLogic.TypeEnums.ENUM_DEFINITION, cur_node.getUserObject().toString()), childNode));
                window_obj.insert_node(childNode, cur_node, 0);
                window_obj.edit_node(childNode);
                break;
            case TREE_SCHEMA_INSERT_SUB_SCHEMA:
                childNode = new javax.swing.tree.DefaultMutableTreeNode("Dummy");
                DataLogic.TypeInfo type_info = (DataLogic.TypeInfo)mItem.get_info_object();
                if (type_info.get_type() == DataLogic.TypeEnums.DATA_TYPE) {
                    childNode.setUserObject(new UI.NodeData((DataLogic.TypeInfo)type_info, childNode));
                } else {
                    // Its an enum definition
                    DataLogic.SchemaInfo schema_info = new DataLogic.SchemaInfo(type_info.get_name(), 0, 0, DataLogic.TypeEnums.DATA_TYPE);
                    schema_info.set_schema_enumeration(cur_node.getUserObject().toString(), type_info.get_name(), "Unknown");
                    childNode.setUserObject(new UI.NodeData((DataLogic.TypeInfo)schema_info, childNode));
                }                
                window_obj.insert_node(childNode, cur_node, 0);
                window_obj.edit_node(childNode);
                break;
            case TREE_ENUM_DEFINITION_NEW_ENUM:
                childNode = new javax.swing.tree.DefaultMutableTreeNode();
                childNode.setUserObject(new UI.NodeData(new DataLogic.EnumsInfo("Enumeration value", 0, DataLogic.TypeEnums.ENUM_VALUE, cur_node.getUserObject().toString()), childNode));
                window_obj.insert_node(childNode, cur_node, 0);
                window_obj.edit_node(childNode);
                break;
            case TREE_NODE_DELETE_DATA_TYPE:
                if (window_obj.reference_to_schema_exists(cur_node)) {
                    System.out.println("Node removal denied");
                    javax.swing.JOptionPane.showMessageDialog(null, "Cannot Delete!! Schema in use.");
                } else {
                    window_obj.remove_node(cur_node);
                    //System.out.println("Removed node");
                }
                break;
            case TREE_NODE_DELETE_ENUM_VALUE:
                if (window_obj.reference_to_enum_val_exists(cur_node) == DataLogic.TypeEnums.ENUM_VALUE) {
                    javax.swing.JOptionPane.showMessageDialog(window_obj, "Cannot delete!! Enumeration value in use.");
                } else {
                    window_obj.remove_node(cur_node);
                }
                break;
            case TREE_NODE_DELETE_ENUM_DEFINITION:
                DataLogic.TypeEnums return_value = window_obj.reference_to_enum_exists(cur_node, "DUMMY");
                
                if (return_value == DataLogic.TypeEnums.ENUM_DEFINITION ||
                        return_value == DataLogic.TypeEnums.ENUM_VALUE) {
                    javax.swing.JOptionPane.showMessageDialog(window_obj, "Cannot delete!! Enumeration definition in use.");
                } else {
                    window_obj.remove_node(cur_node);
                }
                break;
            case TREE_NODE_DELETE_ENUM_MODULE:
                DataLogic.TypeEnums return_module_value = window_obj.reference_to_enum_module_exists(cur_node, "DUMMY", "DUMMY");
                if (return_module_value != DataLogic.TypeEnums.TYPE_UNDEFINED) {
                    javax.swing.JOptionPane.showMessageDialog(window_obj, "Cannot delete!! Enumeration module in use.");
                } else {
                    window_obj.remove_node(cur_node);
                }
                break;
                
            case TREE_NODE_DELETE:
                window_obj.remove_node(cur_node);
                break;
                
            case TREE_NODE_EDIT:
                childNode = (javax.swing.tree.DefaultMutableTreeNode)mItem.get_info_object();
                window_obj.edit_node(childNode);
                break;
            case TREE_NODE_MOVE_UP:
                parentNode = (javax.swing.tree.DefaultMutableTreeNode)cur_node.getParent();
                node_index = parentNode.getIndex(cur_node);
                window_obj.remove_node(cur_node);
                window_obj.insert_node(cur_node, parentNode, (node_index - 1));
                break;
            case TREE_NODE_MOVE_DOWN:
                parentNode = (javax.swing.tree.DefaultMutableTreeNode)cur_node.getParent();
                node_index = parentNode.getIndex(cur_node);
                window_obj.remove_node(cur_node);
                window_obj.insert_node(cur_node, parentNode, (node_index + 1));
                break;               
                
            case TREE_SCHEMA_MODULE_MENU_ITEM_SAVE_MODULE:
            case TREE_ENUM_MODULE_MENU_ITEM_SAVE_MODULE:
                System.out.println("Save module TBD");
                break;
            default:
                break;
        }
        
    }
}
